package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.entity.GridQrCode;
import com.easylinkin.linkappapi.grid.entity.GridQrCodeRecord;
import com.easylinkin.linkappapi.grid.models.dto.GridQrCodeDTO;
import com.easylinkin.linkappapi.grid.models.dto.GridQrCodeRelationDTO;
import com.easylinkin.linkappapi.grid.models.dto.UpdateGridQrCode;
import com.easylinkin.linkappapi.grid.models.query.GridQrCodeQuery;
import com.easylinkin.linkappapi.grid.models.query.GridQrCodeRecordQuery;
import com.easylinkin.linkappapi.grid.models.vo.GridQrCodeInfoVO;
import com.easylinkin.linkappapi.grid.models.vo.GridQrCodeVO;
import com.easylinkin.linkappapi.grid.service.IGridQrCodeRecordService;
import com.easylinkin.linkappapi.grid.service.IGridQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;


/**
 * 网格二维码管理 前端控制器
 *
 * @author
 * @since 2024-05-27
 */
@Api(tags = "网格二维码管理")
@RestController
@RequestMapping("/grid/gridQrCode")
public class GridQrCodeController {
    @Autowired
    private IGridQrCodeService gridQrCodeService;
    @Autowired
    private IGridQrCodeRecordService gridQrCodeRecordService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridQrCodeQuery> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        return this.gridQrCodeService.queryPageList(requestModel);
    }

    /**
     * 创建二维码编号
     *
     * @return site.morn.rest.RestMessage
     * @author
     * @date 2024/5/27 17:08
     */
    @RequestMapping("/createCode")
    public RestMessage createCode() {
        return RestBuilders.successBuilder(this.gridQrCodeService.createCode()).build();
    }

    /**
     * 保存
     *
     * @param gridQrCode
     * @return
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/save")
    public RestMessage save(@Validated @RequestBody GridQrCodeDTO gridQrCode) {
        return this.gridQrCodeService.add(gridQrCode);
    }

    /**
     * 保存
     *
     * @param gridQrCode
     * @return
     */
    @ApiOperation(value = "更新状态", notes = "更新状态")
    @PostMapping(value = "/updateStatus")
    public RestMessage updateStatus(@Validated @RequestBody UpdateGridQrCode gridQrCode) {
        GridQrCode gridQrCode1 = BeanUtil.copyProperties(gridQrCode, GridQrCode.class);
        boolean b = this.gridQrCodeService.updateById(gridQrCode1);
        if (b) {
            return RestBuilders.successBuilder().message("更新成功").build();
        }
        return RestBuilders.errorBuilder().message("更新失败").build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridQrCodeService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        GridQrCode gridQrCode = gridQrCodeService.getById(id);
        if (gridQrCode == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        GridQrCodeInfoVO gridQrCodeInfoVO = BeanUtil.copyProperties(gridQrCode, GridQrCodeInfoVO.class);
        int count = this.gridQrCodeRecordService.count(Wrappers.<GridQrCodeRecord>lambdaQuery().eq(GridQrCodeRecord::getCode, gridQrCode.getCode()));
        gridQrCodeInfoVO.setCount(count);
        return RestBuilders.successBuilder().data(gridQrCodeInfoVO).build();
    }

    @ApiOperation(value = "关联网格", notes = "关联网格")
    @PostMapping(value = "/relevance")
    public RestMessage relevance(@Validated @RequestBody GridQrCodeRelationDTO gridQrCodeRelationDTO) {
        return this.gridQrCodeService.relevance(gridQrCodeRelationDTO);
    }

    @ApiOperation(value = "取消关联网格", notes = "取消关联网格")
    @GetMapping(value = "/disassociate")
    public RestMessage disassociate(@RequestParam("id") String id) {
        return this.gridQrCodeService.disassociate(id);
    }

    @ApiOperation(value = "获取使用记录", notes = "获取使用记录")
    @PostMapping(value = "/getUseList")
    public RestMessage getUseList(@RequestBody RequestModel<GridQrCodeRecordQuery> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        return this.gridQrCodeService.getUseList(requestModel);
    }

    @ApiOperation(value = "获取下拉框数据", notes = "获取下拉框数据")
    @GetMapping(value = "/select")
    public RestMessage querySelectList() {
        return this.gridQrCodeService.querySelectList();
    }

    @ApiOperation(value = "根据code查询二维码信息", notes = "根据code查询二维码信息")
    @GetMapping(value = "/getByCode")
    public RestMessage getByCode(@RequestParam("code") String code) {
        GridQrCodeVO data = gridQrCodeService.getByCode(code);
        return RestBuilders.successBuilder().data(data).build();
    }
}
